package pl.edu.pw.stud.ltrzaska.init;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;

import pl.edu.pw.stud.ltrzaska.config.Const;
import pl.edu.pw.stud.ltrzaska.mvc.controller.Controller;
import pl.edu.pw.stud.ltrzaska.mvc.model.Model;
import pl.edu.pw.stud.ltrzaska.mvc.view.AbstractViewPanel;
import pl.edu.pw.stud.ltrzaska.mvc.view.MainWindowFrame;
import pl.edu.pw.stud.ltrzaska.mvc.view.UserMessage;
import pl.edu.pw.stud.ltrzaska.mvc.view.UserMessageLevel;
import pl.edu.pw.stud.ltrzaska.mvc.view.ViewEvent;

/**
 * Klasa rozpoczynajaca dzialanie aplikacji.
 * @author luk
 *
 */
public class Main {
	private static final Logger LOGGER = Logger.getLogger(Main.class);

	BlockingQueue<ViewEvent> queue;
	Controller controller;
	Model model;
	MainWindowFrame frame;

	private Main() {
		init();
	}

	private void init() {
		queue = new LinkedBlockingQueue<ViewEvent>();
		controller = new Controller(queue);

		frame = new MainWindowFrame(Const.APP_WINDOW_TITLE, queue);
		frame.setVisible(true);

		model = new Model();
		controller.setDefaultModel(model);

		for (AbstractViewPanel vp : frame.getViewPanels()) {
			controller.addView(vp);
			vp.setModel(model);
		}

		Thread t = new Thread(controller);
		t.start();
	}

	public static void main(String[] args) {
		LOGGER.debug("Start aplikacji.");
		Main r = new Main();

		r.run();
	}

	private void run() {
		UserMessage um1 = new UserMessage(UserMessageLevel.INFO, "Start aplikacji");
		model.addUserMessage(um1);
	}
}
